package com.example.demo.utils;

import com.alibaba.otter.canal.protocol.CanalEntry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.annotation.Id;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author <a href="hedong.wu@baozun.com">wuhedong</a>
 * @Date 2020/5/26 17:32
 **/
@Slf4j
public class CanalPropertyUtils {

    public static Map<Class<?>, Map<String, Field>>
            classMapConcurrentHashMap = new ConcurrentHashMap<>();

    public static void printColumn(List<CanalEntry.Column> columns) {
        for (CanalEntry.Column column : columns) {
            log.warn(column.getName() + " : " + column.getValue() + "    update=" + column.getUpdated() + "   type:" + column.getMysqlType());
        }
    }

    /**
     * Canal 中的数据  解析
     * @param isFilter 是否过滤未更新的字段
     */
    public static <T> T makeDomain(List<CanalEntry.Column> columns, Class<T> tClass,boolean isFilter) {
        try {
            T t = tClass.newInstance();
            Map<String, Field> mapByClass = getMapByClass(tClass);
            for (CanalEntry.Column column : columns) {
                String name = column.getName();
                Field field = mapByClass.get(name);
                if (field == null) {
                    continue;
                }
                if (isFilter && !column.getUpdated() && !field.isAnnotationPresent(Id.class)) {
                    continue;
                }
                field.set(t, makeObjectByStringAndField(column.getValue(), field));
            }
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 字符串转化为我们想要的
     */
    static Object makeObjectByStringAndField(String value, Field field) throws ParseException {
        Class<?> type = field.getType();
        if (type == String.class) {
            return value;
        }
        if (type == Date.class) {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value);
        }
        if (type == Long.class) {
            return Long.valueOf(value);
        }
        if (type == Boolean.class) {
            return "0".equals(value) ? Boolean.FALSE : Boolean.TRUE;
        }
        if (type == Integer.class) {
            return Integer.valueOf(value);
        }
        throw new UnsupportedOperationException();
    }

    /**
     * 分解class
     */
    static Map<String, Field> getMapByClass(Class<?> tClass) {
        Map<String, Field> fieldMap = classMapConcurrentHashMap.get(tClass);
        if (fieldMap != null) {
            return fieldMap;
        }
        synchronized (CanalPropertyUtils.class) {
            fieldMap = classMapConcurrentHashMap.get(tClass);
            if (fieldMap != null) {
                return fieldMap;
            }
            fieldMap = new HashMap<>(8);
            Field[] declaredFields = tClass.getDeclaredFields();
            for (Field field : declaredFields) {
                //如果标记为 可以被使用
                if (field.isAnnotationPresent(Id.class)) {
                    field.setAccessible(true);
                    fieldMap.put(field.getName(), field);
                    continue;
                }
                org.apache.solr.client.solrj.beans.Field declaredAnnotation = field.getDeclaredAnnotation(org.apache.solr.client.solrj.beans.Field.class);
                if (declaredAnnotation != null) {
                    field.setAccessible(true);
                    fieldMap.put(declaredAnnotation.value(), field);
                }
            }
            classMapConcurrentHashMap.put(tClass, fieldMap);
        }
        return fieldMap;
    }

}
